<?php

namespace app\shopapi\model;

use fast\Date;

class Stat
{

    /**
     * Notes:type  0是今日,1近七天,2近三十天,3自定义时间
     * stat_chart 0不查,1交易总金额统计图,2交易笔数,3退款金额,4总顾客,5新客,6回头客
     * User: gai
     * Date: 2024/11/2
     * Time: 15:24
     */
    public function report($shop_id,$type,$start_date,$end_date,$stat_chart)
    {
        $where = ['shop_id'=>$shop_id,'pay_status'=>1];

        if ($type==0){
            $startDate = Date::unixtime('day', 0);
            $endDate =  Date::unixtime('day', 0, 'end');
        } elseif ($type==1){
            $startDate =  Date::unixtime('day', -7);
            $endDate =  Date::unixtime('day', 0, 'end');
        } elseif ($type==2){
            // 获取近三十天开始时间
            $startDate =  Date::unixtime('day', -30);
            $endDate =  Date::unixtime('day', 0, 'end');
        } elseif ($type==3){
            $startDate = strtotime($start_date);
            $endDate = strtotime($end_date);
        }


        $where['createtime'] = ['between', [$startDate, $endDate]];

        $user_ids = ShopBills::where($where)->group('user_id')->column('user_id');

        //-----------------------------------数据概览
        //交易总额
        $total_trading_volume = ShopBills::where($where)->sum('arrival_amount');
        //交易笔数
        $total_order_count = ShopBills::where($where)->count();
        //退款金额
        $total_refund_money = ShopBills::where($where)->where('refund_status',1)->sum('arrival_amount');

        //总顾客
        $total_people_count = ShopBills::where($where)->group('user_id')->count();

        //新客
        //$total_new_people_count = ShopBills::where($where)->group('user_id')->count();
        $total_new_people_count = 0;
        //回头客
        $total_old_people_count = 0;
        foreach ($user_ids as $k=>$v){
            $count = ShopBills::where('user_id',$v)->where('createtime','>',$startDate)->find();
            if (!$count){
                $total_new_people_count++;
            } else {
                $total_old_people_count++;
            }
        }

        $data_summary = [
            'total_trading_volume'=>$total_trading_volume,//交易总金额
            'total_order_count'=>$total_order_count,//交易笔数
            'total_refund_money'=>$total_refund_money,//退款金额
            'total_people_count'=>$total_people_count,//总顾客
            'total_new_people_count'=>$total_new_people_count,//新客
            'total_old_people_count'=>$total_old_people_count,//回头客
        ];
        //--------------------------------------结算明细
        //手续费
        $handling_fee = ShopBills::where($where)->sum('service_charge');
        $discount_offer = ShopBills::where($where)->sum('discount_offer');
        //收款金额
        $collection_amount = bcsub($total_trading_volume,$total_refund_money,2);
        //应结算金额
        //手续费+折扣优惠
        $deducted = bcadd($handling_fee,$discount_offer,2);
        $settlement_amount = bcsub($collection_amount,$deducted,2);

        $settlement_data = [
            'collection_amount'=>$collection_amount,//收款金额
            'total_trading_volume'=>$total_trading_volume,//交易总金额
            'total_refund_money'=>$total_refund_money,//退款金额
            'settlement_amount'=>$settlement_amount,//应结算金额
            'handling_fee'=>$handling_fee,//手续费
            'discount_offer'=>$discount_offer,//折扣优惠
        ];
        //------------------------下单频次
        $order_count_1 = 0;
        $order_count_2 = 0;
        $order_count_3 = 0;

        foreach ($user_ids as $k=>$v){
            $user_where = ['user_id'=>$v];
            $user_where['createtime'] = ['between', [$startDate, $endDate]];
            $count = ShopBills::where($user_where)->count();
            if ($count==1){
                $order_count_1++;
            } elseif ($count>=2||$count<=3){
                $order_count_2++;
            } elseif ($count>3){
                $order_count_3++;
            }
        }
        $order_frequency = [
            'order_count_1'=>$order_count_1,//下单频次1次
            'order_count_2'=>$order_count_2,//下单频次2-3次
            'order_count_3'=>$order_count_3,//下单频次3次以上
        ];
        //----------------------统计图
        $column = [];
        for ($time = $startDate; $time <= $endDate;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userdata = [];

        if ($stat_chart>0){
            //1交易总金额统计图,2交易笔数,3退款金额,4总顾客,5新客,6回头客
            foreach ($column as $k=>$v){
                $start_time = strtotime($v);
                $end_time = strtotime($v. "23:59:59");
                switch ($stat_chart) {
                    case 1:
                        $data_count = ShopBills::where(['shop_id'=>$shop_id])
                            ->where('createtime','between',[$start_time,$end_time])
                            ->sum('arrival_amount');
                        break;
                    case 2:
                        $data_count = ShopBills::where(['shop_id'=>$shop_id])
                            ->where('createtime','between',[$start_time,$end_time])
                            ->count();
                        break;
                    case 3:
                        $data_count = ShopBills::where(['shop_id'=>$shop_id])
                            ->where('refund_status',1)
                            ->sum('arrival_amount');
                        break;
                    case 4:
                        $data_count = ShopBills::where(['shop_id'=>$shop_id])->group('user_id')->count();
                        break;
                    case 5:
                        $data_count = 0;
                        foreach ($user_ids as $kk=>$vv){
                            $count = ShopBills::where('user_id',$vv)->where('createtime','>',$start_time)->find();
                            if (!$count){
                                $data_count++;
                            }
                        }
                        break;
                    case 6:
                        $data_count = 0;
                        foreach ($user_ids as $kk=>$vv){
                            $count = ShopBills::where('user_id',$vv)->where('createtime','>',$start_time)->find();
                            if ($count){
                                $data_count++;
                            }
                        }
                        break;
                    default:
                }

                array_push($userdata,$data_count);
            }
        }

        $chart_data = [
            'date'=>$column,
            'data'=>$userdata
        ];
        //dump($userlist);die;

        $data = [
            'data_summary'=>$data_summary,
            'order_frequency'=>$order_frequency,
            'settlement_data'=>$settlement_data,
            'chart_data'=>$chart_data
        ];

        return $data;
    }
}